package com.nl.controller.xuyi.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.util.StringUtil;
import com.nl.controller.xuyi.bean.XuYiCheckInfoBo;
import com.nl.controller.xuyi.bean.xuyiline.XuYiLineInfo;
import com.nl.controller.xuyi.bean.XuYiPointInfo;
import com.nl.controller.xuyi.bean.xuyiline.LineInfo;
import com.nl.controller.xuyi.bean.xuyiline.QryLineInfoReq;
import com.nl.controller.xuyi.bean.xuyiline.QryLineInfoWithMgrIdReq;
import com.nl.controller.xuyi.bean.xuyiline.TreeInfo;
import com.nl.controller.xuyi.bean.xuyimap.QueryRangePo;
import com.nl.controller.xuyi.mapper.XuYiCheckMapper;
import com.nl.controller.xuyi.mapper.XuYiLineMapper;
import com.nl.controller.xuyi.mapper.XuYiMapMapper;
import com.nl.controller.xuyi.mapper.XuYiPointMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangheng
 * @Desc:
 * @Date: 15:18 2020/9/1
 * @Modified By:
 */
@Service
@Slf4j
public class XuYiLineService {
    @Autowired
    private XuYiLineMapper xuYiLineMapper;
    @Autowired
    private XuYiMapMapper xuYiMapMapper;
    @Autowired
    private XuYiPointMapper xuYiPointMapper;
    @Autowired
    private XuYiCheckMapper xuYiCheckMapper;
    
    public Response qryLineInfoNew(QryLineInfoReq req){
        List<LineInfo> lineInfos = xuYiLineMapper.qryLineInfo(req.getPointName());
        Map<String,List<String>> map = new HashMap<>();
        List<TreeInfo> result = new ArrayList<>();
        int count = 0;
        int id = 1;
        if (!CollectionUtils.isEmpty(lineInfos)){
            for (LineInfo lineInfo: lineInfos) {
                String pathName = lineInfo.getPathName();
                String regionName = lineInfo.getRegionName();
                if (map.containsKey(pathName)){
                    List<String> regionList = map.get(pathName);
                    if (regionList.contains(regionName)){
                        int index = regionList.indexOf(regionName) - 1;
                        int parseInt = Integer.parseInt(regionList.get(0));
                        List<TreeInfo> data = result.get(parseInt).getChildren();
                        TreeInfo treeInfo =data.get(index);
                        treeInfo.setField(treeInfo.getField()+"+"+lineInfo.getItude());
                        data.set(index, treeInfo);
                        TreeInfo parentTreeInfo = result.get(parseInt);
                        parentTreeInfo.setChildren(data);
                        result.set(parseInt,parentTreeInfo);
                    } else {
                        int parseInt = Integer.parseInt(regionList.get(0));
                        TreeInfo parentTreeInfo = result.get(parseInt);
                        List<TreeInfo> data = parentTreeInfo.getChildren();
                        regionList.add(regionName);
                        map.put(pathName,regionList);

                        data.add(new TreeInfo(regionName,id,lineInfo.getItude(),new ArrayList<>()));
                        id++;
                        parentTreeInfo.setChildren(data);
                    }
                } else {
                    List<String> list = new ArrayList<>();
                    list.add(String.valueOf(count));
                    list.add(regionName);
                    count++;
                    map.put(pathName,list);
                    TreeInfo treeInfo = new TreeInfo();
                    treeInfo.setId(id++);
                    treeInfo.setTitle(pathName);
                    List<TreeInfo> data = new ArrayList<>();
                    data.add(new TreeInfo(regionName,id,lineInfo.getItude(),new ArrayList<>()));
                    id++;
                    treeInfo.setChildren(data);
                    result.add(treeInfo);
                }
            }
        }
        return ResponseUtil.sucess(result);
    }
    public Response qryLineInfo(QryLineInfoReq req){
    	List<LineInfo> lineInfos = xuYiLineMapper.qryLineInfoNew(req.getPointName());
        List<TreeInfo> result = new ArrayList<>();
        int id = 1;
        List<String> lineNames = new ArrayList<>();
        if (!CollectionUtils.isEmpty(lineInfos)) {
            for (LineInfo lineInfo : lineInfos) {
                String itude = lineInfo.getItude().replaceAll("\\|", "+");
                String pathName = lineInfo.getPathName();
                String regionName = lineInfo.getRegionName();
                if (lineNames.contains(pathName)){
                    result.get(result.size()-1).getChildren().add(new TreeInfo(regionName,id,itude,new ArrayList<>()));
                    id++;
                }else {
                    TreeInfo treeInfo = new TreeInfo(pathName,id,null,new ArrayList<>());
                    id++;
                    treeInfo.getChildren().add(new TreeInfo(regionName,id,itude,new ArrayList<>()));
                    id++;
                    lineNames.add(pathName);
                    result.add(treeInfo);
                }
            }
        }
        return ResponseUtil.sucess(result);
    }


    public Response qryLineInfoWithMgrId(QryLineInfoWithMgrIdReq req) {
        QryLineInfoReq qryLineInfo = new QryLineInfoReq();
        qryLineInfo.setPointName("");
        Response lineInfo = qryLineInfo(qryLineInfo);
        List<TreeInfo> result = (List<TreeInfo>) lineInfo.getData();
        List<String> mgrIds = new ArrayList<>();
        mgrIds.add(req.getMgrId());
        List<QueryRangePo> rangePos = xuYiMapMapper.queryRange(mgrIds);
        QueryRangePo rangePo = rangePos.get(0);
        String regionTypeValue = rangePo.getRegionTypeValue();
        List<String> linePointNames = JSON.parseArray(regionTypeValue.split("%_%")[1], String.class);
        Map<String,List<String>> map = new HashMap<>();
        for (String linePointName: linePointNames){
            String[] split = linePointName.split("-");
            String lineName = split[0];
            String pointName = split[1];
            if (!map.containsKey(lineName)){
                List<String> list = new ArrayList<>();
                list.add(pointName);
                map.put(lineName,list);
            } else {
                List<String> list= map.get(lineName);
                list.add(pointName);
                map.put(lineName,list);
            }
        }
        for (TreeInfo treeInfo : result){
            if (map.containsKey(treeInfo.getTitle())){
//                treeInfo.setChecked(true);
                List<String> list = map.get(treeInfo.getTitle());
                for (TreeInfo children : treeInfo.getChildren()){
                    if (list.contains(children.getTitle())){
                        children.setChecked(true);
                    }
                }
            }
        }
        return  ResponseUtil.sucess(result);
    }

    public PageInfo<XuYiLineInfo> findLinesInfo(int page, int limit, String lineName) {
        PageHelper.startPage(page, limit);
        List<XuYiLineInfo> linesList = new ArrayList<XuYiLineInfo>();
        XuYiLineInfo xuYiLineInfo = new XuYiLineInfo();
        xuYiLineInfo.setName(lineName);
        linesList= xuYiLineMapper.findLineInfo(xuYiLineInfo);
        PageInfo<XuYiLineInfo> pageInfo = new PageInfo<XuYiLineInfo>(linesList);
        return pageInfo;
    }

    public List<String> findLineNameList(String lineName) {
        List<String> lineNameList = new ArrayList<>();
        lineNameList = xuYiLineMapper.findLineNameList(lineName);
        return lineNameList;
    }

    public int lineRelPointAdd(String lineName, String pointNameList, String operatorTel) {
        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();
        String lineId = xuYiLineMapper.findLineSeq();
        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        String pointId  = "";
        String[] pointNames;
        int insertCheckResult = 0;
        if(!StringUtil.isNull(pointNameList)){
            pointNames = pointNameList.split(",");
            for(String pointName : pointNames){
                xuYiPointInfo.setName(pointName);
                xuYiPointInfo=xuYiPointMapper.findPointInfo(xuYiPointInfo).get(0);
                if(StringUtil.isNull(pointId)){
                    pointId = xuYiPointInfo.getId();
                }else{
                    pointId = pointId +","+xuYiPointInfo.getId();
                }
            }
            xuYiCheckInfoBo.setLineId(lineId);
            xuYiCheckInfoBo.setLineName(lineName);
            xuYiCheckInfoBo.setPointId(pointId);
            xuYiCheckInfoBo.setPointName(pointNameList);
            xuYiCheckInfoBo.setCheckState("0");
            xuYiCheckInfoBo.setType("4");
            xuYiCheckInfoBo.setOperator(operatorTel);
            insertCheckResult = xuYiCheckMapper.addLinePointCheckInfo(xuYiCheckInfoBo);
        }
//        int insertRelResult = 0;
//        int insertRelCount = 0;
//        String lineId = xuYiLineMapper.findLineSeq();
//        XuYiLineInfo xuYiLineInfo = new XuYiLineInfo();
//        xuYiLineInfo.setId(lineId);
//        xuYiLineInfo.setName(lineName);
//        xuYiLineInfo.setOperatorTel(operatorTel);
//        xuYiLineInfo.setState("1");
//        xuYiLineInfo.setCheckState("2");
//        //新路线信息插入路线表
//        xuYiLineMapper.addLineInfo(xuYiLineInfo);
//
//        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
//        String pointId  = "";
//        String[] pointNames;
//        if(!StringUtil.isNull(pointNameList)){
//            pointNames = pointNameList.split(",");
//            for(String pointName : pointNames){
//                xuYiPointInfo.setName(pointName);
//                xuYiPointInfo=xuYiPointMapper.findPointInfo(xuYiPointInfo).get(0);
//                pointId =xuYiPointInfo.getId();
//                insertRelResult = xuYiLineMapper.addLinePointRel(lineId,pointId,operatorTel);
//                if(insertRelResult>0){
//                    insertRelCount++;
//                }
//            }
//        }
        return insertCheckResult;
    }

    public XuYiLineInfo findLineInfoById(String lineId){
        XuYiLineInfo xuYiLineInfo = xuYiLineMapper.findLineInfoById(lineId);
        return xuYiLineInfo;
    }

    public  List<String> findPointRelByLineId(String lineId) {
        List<String> pointNameList = xuYiLineMapper.findPointNamesByLineId(lineId);
        return  pointNameList;
    }

    public int lineRelPointModify(String lineId, String lineName, String pointNameList, String operatorTel) {
        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();
        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
        String pointId  = "";
        String[] pointNames;
        int insertCheckResult = 0;
        if(!StringUtil.isNull(pointNameList)){
            pointNames = pointNameList.split(",");
            for(String pointName : pointNames){
                xuYiPointInfo.setName(pointName);
                xuYiPointInfo=xuYiPointMapper.findPointInfo(xuYiPointInfo).get(0);
                if(StringUtil.isNull(pointId)){
                    pointId = xuYiPointInfo.getId();
                }else{
                    pointId = pointId +","+xuYiPointInfo.getId();
                }
            }
            xuYiCheckInfoBo.setLineId(lineId);
            xuYiCheckInfoBo.setLineName(lineName);
            xuYiCheckInfoBo.setPointId(pointId);
            xuYiCheckInfoBo.setPointName(pointNameList);
            xuYiCheckInfoBo.setCheckState("0");
            xuYiCheckInfoBo.setType("5");
            xuYiCheckInfoBo.setOperator(operatorTel);
            insertCheckResult = xuYiCheckMapper.addLinePointCheckInfo(xuYiCheckInfoBo);
        }

//        int insertRelResult = 0;
//        int insertRelCount = 0;
//
//        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();
//        String pointId  = "";
//        String[] pointNames;
//        if(!StringUtil.isNull(pointNameList)){
//            pointNames = pointNameList.split(",");
//            //先清除表中历史关联数据
//            int clearCount = xuYiLineMapper.clearHisRel(lineId);
//            log.info("已清理历史数据数量"+clearCount);
//            for(String pointName : pointNames){
//                xuYiPointInfo.setName(pointName);
//                xuYiPointInfo=xuYiPointMapper.findPointInfo(xuYiPointInfo).get(0);
//                pointId =xuYiPointInfo.getId();
//                //插入新数据
//                insertRelResult = xuYiLineMapper.addLinePointRel(lineId,pointId,operatorTel);
//                if(insertRelResult>0){
//                    insertRelCount++;
//                }
//            }
//        }
        return insertCheckResult;

    }

    public int deleteLine(String lineId,String lineName,String operatorTel) {
        XuYiCheckInfoBo xuYiCheckInfoBo = new XuYiCheckInfoBo();
        int insertCheckResult = 0;
        try {
            xuYiCheckInfoBo.setLineId(lineId);
            xuYiCheckInfoBo.setLineName(lineName);
            xuYiCheckInfoBo.setCheckState("0");
            xuYiCheckInfoBo.setType("6");
            xuYiCheckInfoBo.setOperator(operatorTel);
            insertCheckResult = xuYiCheckMapper.addLinePointCheckInfo(xuYiCheckInfoBo);
        } catch (Exception e) {
            log.info("删除路线插入审核表发生异常");
            e.printStackTrace();
        }
        return insertCheckResult;
    }

    public List<XuYiLineInfo> findLinesInfoByName(String lineName) {
        List<XuYiLineInfo> lineInfos;
        XuYiLineInfo xuYiLineInfo = new XuYiLineInfo();
        xuYiLineInfo.setName(lineName);
        lineInfos= xuYiLineMapper.findLineInfo(xuYiLineInfo);
        return lineInfos;
    }
}
